package com.nerve.android.cloudoffice.app.activity.notify;

import org.nerve.android.NerveActivityGroup;
import org.nerve.android.annotation.Acvitity;
import org.nerve.android.annotation.ViewOnId;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.widget.EditText;

import com.nerve.android.cloudoffice.R;
import com.nerve.android.cloudoffice.app.NavigationActivity;
import com.nerve.android.cloudoffice.app.group.IndexGroupActivity;
import com.nerve.android.cloudoffice.common.Const;
import com.nerve.android.cloudoffice.common.Reply;
import com.nerve.android.cloudoffice.domain.Notify;
import com.nerve.android.cloudoffice.service.data.NotifyData;

/**
 * @project: Cloudoffice
 * @file: AddNotifyActivity.java
 * @package: com.nerve.android.cloudoffice.app.activity.notify
 * @description:
 *
 * @author: 集成显卡	1053214511@qq.com
				IBM GDC, zengxm@cn.ibm.com
 * @date&time: Aug 15, 2013
 * @change log:
 */
@SuppressLint("HandlerLeak")
@Acvitity(layout=R.layout.activity_notify_add)
public class AddNotifyActivity extends NavigationActivity{

	@ViewOnId(id=R.id.name)
	private EditText titleET;
	@ViewOnId(id=R.id.content)
	private EditText contentET;
	
	private Handler handler = new Handler(){
		public void handleMessage(Message msg){
			hideProgressDialog();
			switch(msg.what){
			case WORKER_OK:
				toast("保存成功");
				onNavigateBackButtonClick();
				break;
			case WORKER_FAIL:
				toast(Const.ERROR);
				break;
			}
		}
	};
	
	@Override
	protected void initUI() {
		super.initUI();
		
		setNavTitle("新建通知公告");
		customNavigateRightButton("保存", true);
	}
	
	@Override
	protected void onNavigateBackButtonClick() {
		NerveActivityGroup group = getActivityGroup(IndexGroupActivity.class);
		group.back();
	}
	
	@Override
	protected void onNavigateRightButtonClick() {
		if(isEmpty(titleET)){
			toast("标题不能为空");
			return ;
		}
		
		if(isEmpty(contentET)){
			toast("内容不能为空");
			return ;
		}
		
		String title = getText(titleET);
		String content = getText(contentET);
		
		showProgressDialog(null, "正在保存公告...");
		new AddWorker().execute(title, content);
	}
	
	/**
	 * @project: Cloudoffice
	 * @file: AddNotifyActivity.java
	 * @package: com.nerve.android.cloudoffice.app.activity.notify
	 * @description:
	 *
	 * @author: 集成显卡	1053214511@qq.com
					IBM GDC, zengxm@cn.ibm.com
	 * @date&time: Aug 16, 2013
	 * @change log:
	 */
	private class AddWorker extends AsyncTask<String, Void, Integer>{

		@Override
		protected Integer doInBackground(String... params) {
			Notify n = new Notify();
			n.setTitle(params[0]);
			n.setMessage(params[1]);
			
			Reply re = NotifyData.add(n);
			if(re.isError())
				Const.ERROR = re.getMessage().toString();
			
			return re.isError()? WORKER_FAIL : WORKER_OK;
		}
		
		@Override
		protected void onPostExecute(Integer result) {
			handler.sendEmptyMessage(result);
		}
	}
}